/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package escom.ipn.administrador;

import escom.ipn.modelo.delegate.adminCRPDelegate;
import escom.ipn.modelo.delegate.adminPoliciaCRPDelegate;
import escom.ipn.modelo.delegate.adminPoliciaDelegate;
import escom.ipn.modelo.delegate.adminRadioDelegate;
import escom.ipn.modelo.delegate.adminSectorDelegate;
import escom.ipn.modelo.dto.CRPClass;
import escom.ipn.modelo.dto.PoliciaClass;
import escom.ipn.modelo.dto.RadioClass;
import escom.ipn.modelo.dto.SectorClass;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author db2admin
 */
@WebServlet(name = "insertarPolicia", urlPatterns = {"/insertarPolicia"})
public class insertarPolicia extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        //Declaracion de clases manejadoras
        adminPoliciaDelegate del;
        adminCRPDelegate delCr;
        adminCRPDelegate delCrUpdate;
        adminPoliciaCRPDelegate delC;
        adminSectorDelegate delS;
        adminRadioDelegate delR ;
        PoliciaClass a ;
        SectorClass s ;
        RadioClass r ;
        CRPClass c ;
        List sec;
        List rad;
        List crp;
        
        //Pedir datos
        
       
        try {
            //Regresa "on" si esta activado en caso contrario regresa "null"
            String chk=request.getParameter("chk");
            if(chk != null){
                delCr = new adminCRPDelegate();
                delC = new adminPoliciaCRPDelegate();
                delCrUpdate = new adminCRPDelegate();
                a = new PoliciaClass();
                c = new CRPClass();
                List listaCRP;
                List lista;
                int asig=0;     
                
                //asignar informacion acerca de la patrulla
                String nomcrp=request.getParameter("CRP");
                c.setNombre(nomcrp);
                listaCRP=delCr.getCRPID(c);
                c= (CRPClass)listaCRP.get(0);

                //update del numero de policias dentro del CRP
                lista=delCrUpdate.loadSelect(c);
                c=(CRPClass) lista.get(0);
                asig=c.getAsignado();
                asig+=1;
                c.setAsignado(asig);
                delCrUpdate = new adminCRPDelegate();
                delCrUpdate.asignacion(c);
                
                a.setCrp(c.getId_carro_radio_patrulla());
                a.setNombre(request.getParameter("nombre"));
                a.setAp_paterno(request.getParameter("ap_paterno"));
                a.setAp_materno(request.getParameter("ap_materno"));
                a.setDireccion(request.getParameter("direccion"));
                a.setTelefono(request.getParameter("tel"));
                a.setGrado(request.getParameter("grado"));
                a.setJornada(Integer.parseInt(request.getParameter("jornada")));
                
                if(a!=null){
                    delC.guardarPolicia(a);
                }
                
                
            }else if(chk == null){
                del = new adminPoliciaDelegate();
                delS = new adminSectorDelegate();
                delR = new adminRadioDelegate();
                a = new PoliciaClass();
                s = new SectorClass();
                r = new RadioClass();
                
                //asignar informacion acerca del Sector
                s.setNombre(request.getParameter("Sector"));
                sec=delS.getSectorID(s);
                s=(SectorClass)sec.get(0);
                
                //asignar informacion acerca del Radio
                String radio=request.getParameter("Radio");
                if(radio.equals("Sin Radio Asignado")){
                    r.setNombre(radio);
                    rad=delR.getRadioID(r);
                    r=(RadioClass)rad.get(0);
                }else{
                r.setNombre(radio);
                rad=delR.getRadioID(r);
                r=(RadioClass)rad.get(0);
                r.setAsignado(1);
                delR.asignacion(r);
                }
                
                a.setRADIO_COMUNICACION_id_radio(r.getId());
                a.setSector(s.getId());
                a.setNombre(request.getParameter("nombre"));
                a.setAp_paterno(request.getParameter("ap_paterno"));
                a.setAp_materno(request.getParameter("ap_materno"));
                a.setDireccion(request.getParameter("direccion"));
                a.setTelefono(request.getParameter("tel"));
                a.setGrado(request.getParameter("grado"));
                a.setJornada(Integer.parseInt(request.getParameter("jornada")));
            
                if(a!=null){
                    del.guardarPolicia(a);
                }
                
            }
            String redirectURL = "Administrador/administrador.jsp#tabpolicia";
            response.sendRedirect(redirectURL);  
            
        } catch (SQLException ex) {
           Logger.getLogger(insertarPolicia.class.getName()).log(Level.SEVERE, null, ex);
        }              
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
